<?php

namespace App\Http\Controllers\CmsApi;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Lib\Response;
use App\Http\Controllers\BaseController;
use App\Http\Model\Hint;
use App\Http\Services\RedPacketRainServices;


class InnerRedPacketRainController extends BaseController
{
    protected $redPacketRainService;

    public function __construct()
    {
        $this->redPacketRainService = new RedPacketRainServices();

    }

    /**
     * 红包雨新增
     */
    public function redPacketRainAdd(Request $request)
    {
        $startTime = $request->input('start_time', '');
        $rechargeDiamondNum = (int) $request->input('recharge_diamond_num', 0);
        $allDiamondNum = (int) $request->input('all_diamond_num', 0);
        $packetNum = (int) $request->input('packet_num', 0);
        $minNum = (int) $request->input('min_num', 0);
        $maxNum = (int) $request->input('max_num', 0);
        $adminId = (int) $request->input('admin_id', 0);
        if(empty($startTime) || empty($allDiamondNum) || empty($packetNum) || empty($minNum) || empty($maxNum) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $startTime = date('Y-m-d H:i:00', strtotime($startTime));
        //锁
        $ifLock = redis_lock('REDPACKETRAIN:redPacketRainAdd', 2);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->redPacketRainService->redPacketRainAdd($startTime, $rechargeDiamondNum, $allDiamondNum, $packetNum, $minNum, $maxNum, $adminId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 红包雨编辑
     */
    public function redPacketRainEdit(Request $request)
    {
        $rainId = $request->input('rain_id', 0);
        $startTime = $request->input('start_time', '');
        $rechargeDiamondNum = (int) $request->input('recharge_diamond_num', 0);
        $allDiamondNum = (int) $request->input('all_diamond_num', 0);
        $packetNum = (int) $request->input('packet_num', 0);
        $minNum = (int) $request->input('min_num', 0);
        $maxNum = (int) $request->input('max_num', 0);
        $adminId = (int) $request->input('admin_id', 0);
        if(empty($rainId) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('REDPACKETRAIN:redPacketRainEdit', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->redPacketRainService->redPacketRainEdit($rainId, $startTime, $rechargeDiamondNum, $allDiamondNum, $packetNum, $minNum, $maxNum, $adminId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 红包雨删除
     */
    public function redPacketRainDel(Request $request)
    {
        $rainId = $request->input('rain_id', 0);
        $adminId = (int) $request->input('admin_id', 0);
        if(empty($rainId) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('REDPACKETRAIN:redPacketRainDel', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->redPacketRainService->redPacketRainDel($rainId, $adminId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

}